package com.hos.db.manager;

import com.hos.db.create.sms.SmsTable;
import com.hos.db.entity.SmsEntity;
import com.hos.net.req.sms.SmsReq;
import com.hos.net.res.BaseResponse;
import com.hos.net.res.MsgHint;
import com.hos.operation.manager.SmsUtile;
import com.hos.utile.DLog;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;

import java.util.Date;
import java.util.List;
import java.util.Random;

//短信
public class SMSManager {

    private static SMSManager manager;

    public static SMSManager getInstance() {
        if (manager == null) {
            manager = new SMSManager();
        }
        return manager;
    }

    /**
     * @param jdbcTemplate
     * @param req          发送短信
     * @return 结果
     */
    public String smsSend(JdbcTemplate jdbcTemplate, SmsReq req) {
        String response = new MsgHint("发送失败").toJson();
        String code = getRandom();
        boolean isOk = SmsUtile.getInstance().onSendSms(req.phone, code);
        if (!isOk) {
            return response;
        }
        String sql = "INSERT INTO " + SmsTable.tableName
                + "("
                + SmsTable.tableSMSPhone + ","
                + SmsTable.tableSMSType + ","
                + SmsTable.tableSMSMsg + ","
                + SmsTable.tableSMSSendTime
                + ")"
                + " VALUES (?,?, ?, ?)";
        try {
            int status = jdbcTemplate.update(sql,
                    new Object[]{req.phone, req.type, code, new Date()});
            if (status > 0) {
                BaseResponse<String> baseResponse = new BaseResponse();
                baseResponse.obj = "";
                response = baseResponse.toJson("发送成功");
            }
        } catch (Exception e) {
            DLog.e("发送功：" + e.getMessage());
        }
        return response;
    }

    /**
     * 获取验证码
     *
     * @param jdbcTemplate
     * @param phone        手机号
     * @param type         验证码类型
     * @param code         验证码
     * @return
     */
    public SmsEntity getSms(JdbcTemplate jdbcTemplate, String phone, String type,
                            String code) {
        SmsEntity sms = null;
        String sql = "SELECT * FROM " + SmsTable.tableName + " WHERE "
                + SmsTable.tableSMSPhone + " = " + phone + " and "
                + SmsTable.tableSMSType + " = " + type + " and "
                + SmsTable.tableSMSMsg + " = " + code
                + " ORDER BY " + SmsTable.tableSMSSendTime
                + " desc ";
        List<SmsEntity> datas = smsQuery(jdbcTemplate, sql);
        if (datas != null && datas.size() > 0) {
            sms = datas.get(0);
        }
        return sms;
    }

    /**
     * 删除验证码
     *
     * @param jdbcTemplate
     * @param smsId
     */
    public void deleteSms(JdbcTemplate jdbcTemplate, String smsId) {
        // DELETE FROM table_name [WHERE Clause]
        String sql = "DELETE FROM " + SmsTable.tableName + " WHERE " + SmsTable.tableSMSId + " = " + smsId;
        jdbcTemplate.execute(sql);
    }

    //查询验证码
    private List<SmsEntity> smsQuery(JdbcTemplate jdbcTemplate, String sql) {
        List<SmsEntity> users = null;
        try {
            users = jdbcTemplate.query(sql,
                    ParameterizedBeanPropertyRowMapper.newInstance(SmsEntity.class));
            DLog.e("查询成功：==========");
        } catch (Exception e) {
            DLog.e("查询失败：" + e.getMessage());
        }
        return users;
    }

    //获取随机码
    private String getRandom() {
        Random random = new Random();
        String randoms = "";
        for (int i = 0; i < 4; i++) {
            int randomInt = random.nextInt(9);
            randoms += randomInt;
        }
        return randoms;
    }
}
